<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\api\controller\v1\car;


use app\Request;
use app\services\car\BaseCityServices;
use app\services\car\BasePlaceServices;
use app\services\car\CarBrandServices;
use app\services\car\CarCategoryServices;
use app\services\car\CarOrderServices;
use app\services\car\CarTypeInfoServices;
use app\services\user\UserBillServices;

class PublicController
{
    /**
     * 获取所有显示的城市
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAllCity(){
        /** @var BaseCityServices $cityServices */
        $cityServices = app()->make(BaseCityServices::class);
        $city = $cityServices->getAllCity();
        return app('json')->success($city);
    }

    /**
     * 获取所有城市所有地点
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAllPlace(Request $request){
        $data = $request->getMore([
            ['city_id','']
        ]);
        /** @var BasePlaceServices $placeServices */
        $placeServices = app()->make(BasePlaceServices::class);
        $place = $placeServices->getPlace(['is_del'=>0,'city_id'=>$data['city_id']]);
        return app('json')->success($place);
    }
    /**
     * 获取所有城市所有地点
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get_all_city_place(){
        /** @var BaseCityServices $cityServices */
        $cityServices = app()->make(BaseCityServices::class);
        $city = $cityServices->getAllCity2();
        if($city){
            $placeServices = app()->make(BasePlaceServices::class);
            foreach($city as $k => $v){
                $city[$k]['place'] = $placeServices->getPlace(['is_del'=>0,'city_id'=>$v['id']]);
            }
        }
        return app('json')->success($city);
    }

    /**
     * 获取推荐车型
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getReTypeList(){
        /** @var CarTypeInfoServices $carTypeInfoServices */
        $carTypeInfoServices = app()->make(CarTypeInfoServices::class);
        return app('json')->success($carTypeInfoServices->getIsRecommendCar());
    }

    /**
     * 获取所有车型分类1
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAllCarCategory(Request $request){
        $data = $request->getMore([
            ['city_id',0],
            ['place_id',''],
            ['take_latitude', 'place_id'],
            ['take_longitude', ''],
            ['return_latitude', ''],
            ['return_longitude', ''],
        ]);
        /** @var CarCategoryServices $categoryServices */
        $categoryServices = app()->make(CarCategoryServices::class);
        $brandServices = app()->make(CarBrandServices::class);
        // 分类
        if(!$data['place_id'])return app('json')->fail('店铺参数错误');
        $category = $categoryServices->getAllCategory((int)$data['place_id'],$data);
        // 价格排序
        $price_sort = [
            ['value'=>0,'label'=>'综合排序'],
            ['value'=>1,'label'=>'价格升序'],
            ['value'=>2,'label'=>'价格降序'],
        ];
        // 品牌
        $brand = $brandServices->getAllBrand();
        foreach ($brand as &$item){
            $item['check'] = false;
        }
        // 座位数
        $seat = sys_data('car_seat_sum');
        // 价格区间
        $price = sys_data('car_price_section');
        // 档位
        $gears = sys_data('car_gears_type');
        return app('json')->success(compact('category','price_sort','brand','seat','price','gears'));
    }

    /**
     * 处理取还车时间
     * @param Request $request
     * @return mixed
     */
    public function disposeTime(Request $request){
        $data = $request->getMore([
            ['starStamp',0],
            ['endStamp',0],
            ['starMon',''],
            ['endMon',''],
            ['starDay',0],
            ['endDay',0],
            ['starTime',''],
            ['endTime',''],
            ['is_visit',false],
        ]);
        $week = date("Y-m-d",$data['starStamp']/1000);
        $week2 = date("Y-m-d",$data['endStamp']/1000);
        $getWeek = getWeek($data['starStamp']/1000);
        $getWeek2 = getWeek($data['endStamp']/1000);
        $get_weekend = get_weekend_days($week,$week2,2);
        if($get_weekend['total_days']<=2&&$getWeek=="周五")
        {
            $get_weekend["total_relax"] = 0;
        }elseif($get_weekend['total_days']<=2&&$getWeek=="周六"){
            $get_weekend["total_relax"] = 1;
        }
        $yz = ['3','9','10','16','17','23','24','30','31','37','38','44','45','51','52'];
        $ya = in_array($get_weekend['total_days'],$yz);
        if(($getWeek2=="周六"||$getWeek2=="周日")&&$getWeek!="周五"&&$get_weekend['total_days']>2||$ya==1){
            $get_weekend["total_relax"] =  $get_weekend["total_relax"]-1;
            if( $get_weekend["total_relax"] <0){
                $get_weekend["total_relax"] = 0;
            }
        }
        if(!$data['starStamp'] || !$data['starMon'] || !$data['starDay'] || !$data['starTime']) return app('json')->fail('请选择取车时间');
        if(!$data['endStamp'] || !$data['endMon'] || !$data['endDay'] || !$data['endTime']) return app('json')->fail('请选择还车时间');
        $data['starStamp'] = strtotime(date('Y-m-d',$data['starStamp'] / 1000).' '.$data['starTime']);
        $data['endStamp'] = strtotime(date('Y-m-d',$data['endStamp'] / 1000). ' ' .$data['endTime']);
        if($data['starStamp'] < time()) return app('json')->fail('取车时间不能小于当前时间');
        if($data['starStamp'] > $data['endStamp']) return app('json')->fail('取车时间不能大于还车时间');
        $date = [];
        // 相差天数
        $date['differ_day'] = intval(($data['endStamp']-$data['starStamp'])/86400);
        // 相差小时数，半小时的算1小时
        $date['differ_hour'] = intval(round(($data['endStamp']-$data['starStamp'])%86400/3600));
        // 是否选择了上门送取车
        if($data['is_visit'] == 'true'){
            if(7200 > $data['starStamp'] - time()){
                return app('json')->fail('上门送取车需要提前2小时预约');
            }
        }else{
            if(intval(($data['endStamp']-$data['starStamp'])/3600) < 2){
                return app('json')->fail('用车时间不能小于2小时');
            }
        }
        $date['is_zm'] = $get_weekend['total_relax'];
        $date['starStamp'] = $data['starStamp'];
        $date['endStamp'] = $data['endStamp'];
        return app('json')->success($date);
    }

    /**
     * 获取对应城市下的门店车型信息
     * @param Request $request
     * @param BasePlaceServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCarShopList(Request $request,BasePlaceServices $services){
        $data = $request->getMore([
            ['type_info_id',''],
            ['city_id',''],
            ['stat_time',''],
            ['end_time',''],
            ['take_latitude',''],
            ['take_longitude',''],
            ['return_latitude',''],
            ['return_longitude',''],
        ]);
        if(!$data['type_info_id'] || !$data['city_id']) return app('json')->fail('参数错误');
        return app('json')->success($services->getCarShopList($data));
    }

    /**
     * 获取积分列表
     * @param Request $request
     * @param UserBillServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIntegralList(Request $request,UserBillServices $services){
        $uid = $request->uid();
        return app('json')->success($services->getIntegralList($uid));
    }

    /**
     * 获取用户可使用的优惠券
     * @param Request $request
     * @param CarOrderServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get_use_coupon_list(Request $request,CarOrderServices $services){
        $data = $request->getMore([
           ['price',0]
        ]);
        $uid = $request->uid();
        return app('json')->success($services->get_use_coupon($uid,$data['price']));
    }

    /**
     * 获取配置信息
     */
    public function getclientConfig(){
        $data['shendeng_user_banner'] = sys_data('shendeng_user_banner');
        $data['car_advertising_carousel'] = sys_data('car_advertising_carousel');
        $data['driving_license'] = sys_config('driving_license');
        $data['two_identity_card'] = sys_config('two_identity_card');
        $data['reserve_notice'] = sys_data('reserve_notice');
        $data['cancellation_rule'] = sys_data('cancellation_rule');
        $data['apply_banner_one'] = sys_config('apply_banner_one');
        $data['apply_banner_two'] = sys_config('apply_banner_two');
        $data['max_banner_one_title'] = sys_config('max_banner_one_title');
        $data['min_banner_one_title'] = sys_config('min_banner_one_title');
        $data['max_banner_two_title'] = sys_config('max_banner_two_title');
        $data['min_banner_two_title'] = sys_config('min_banner_two_title');
        $data['min_banner_two_title'] = sys_config('min_banner_two_title');
        $data['is_bottom_img_car'] = sys_config('is_bottom_img_car');
        $data['bottom_img_car'] = sys_config('bottom_img_car');
        $data['routine_name'] = sys_config('routine_name');
        $data['slippage_rule'] = sys_data('slippage_rule');
        return app('json')->success($data);
    }

    /**
     * 判断城市是否开通
     * @param Request $request
     * @param BaseCityServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function isCityOpen(Request $request,BaseCityServices $services){
        $data = $request->getMore([
            ['city_name',''],
        ]);
        if(!$data['city_name']) return app('json')->fail('请选择城市');
        return app('json')->success($services->isCityOpen($data));
    }

    /**
     * 获取门店信息
     * @param Request $request
     * @param BasePlaceServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getStoreInfo(Request $request,BasePlaceServices $services){
        $data = $request->getMore([
            ['store_id',0],
        ]);
        $res = $services->getInfo((int)$data['store_id']);
        return app('json')->success($res);
    }
}
